package com.ruoyi.server.utils;

import com.ruoyi.server.enums.DeleteConstants6;
import com.ruoyi.server.pkg.SendMsg;
import io.netty.channel.ChannelHandlerContext;

import java.util.Objects;
import java.util.Set;

/**
 *  删除工具类
 *
 * @author OpenACloud
 * @create 2024/6/13 14:16
 */
public class DeleteUtil {

    /**
     *  删除好友回执
     *
     * @param ctx
     * @param sendMsg
     */
    public static void deleteFriendRsp(ChannelHandlerContext ctx, SendMsg sendMsg) {
        Set<Long> userFriend = ctx.attr(OnlineUtil.USER_FRIEND).get();
        if(userFriend.contains(sendMsg.RECEIVEID)) {
            userFriend.remove(userFriend);
        }
        ctx.attr(OnlineUtil.USER_FRIEND).set(userFriend);
        SendMsg rspMsg = new SendMsg();
        rspMsg.CMD = DeleteConstants6.DELETE_FRIEND;
        rspMsg.STATUS = DeleteConstants6.DELETE_SUCCESS;
        rspMsg.SENDID = -1L;
        rspMsg.CONTEXT = "删除成功!";
        ctx.channel().writeAndFlush(rspMsg);
        ChannelHandlerContext userCtx = OnlineUtil.ONLINE_MAPS.get(sendMsg.RECEIVEID);
        if(Objects.nonNull(userCtx)){
            Set<Long> userFriend2 = userCtx.attr(OnlineUtil.USER_FRIEND).get();
            if (userFriend2.contains(sendMsg.SENDID)) {
                userFriend2.remove(sendMsg.SENDID);
                userCtx.attr(OnlineUtil.USER_FRIEND).set(userFriend2);
            }
            // TODO 刷新命令
        }
    }
}
